﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc.Html;

namespace System.Web.Mvc
{
    public static class KindExtensions
    {
        /// <summary>
        /// 编辑器脚本引用
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="contentPath"></param>
        /// <returns></returns>
        public static HtmlString KindScript(this UrlHelper helper)
        {
            const string template = " <script src='{0}' type='text/javascript'></script>";

            string url = UrlExtensions.GetUrlString(helper, template, "kindeditor-4.1.4/kindeditor-min.js");
            string lang = UrlExtensions.GetUrlString(helper, template, "kindeditor-4.1.4/lang/zh_CN.js");

            return new HtmlString(url + lang);
        }

        /// <summary>
        /// 编辑器显示模式
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <typeparam name="TProperty"></typeparam>
        /// <param name="htmlHelper"></param>
        /// <param name="expression"></param>
        /// <param name="fileid"></param>
        /// <param name="isSimple"></param>
        /// <param name="htmlAttributes"></param>
        /// <returns></returns>
        public static HtmlString EditSpan<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, System.Linq.Expressions.Expression<Func<TModel, TProperty>> expression,
            object htmlAttributes = null)
        {
            IDictionary<string, object> attributes = HtmlExtensions.GetHtmlAttributes(htmlAttributes);

            HtmlExtensions.SetHtmlAttributes(ref attributes, "style", "width:600px;height:200px;");

            string name = ExpressionHelper.GetExpressionText(expression);
            string fullHtmlFieldName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(name);
            string fileid = htmlHelper.ViewData.Eval(fullHtmlFieldName).ToString();

            TagBuilder builder = new TagBuilder("div");
            builder.MergeAttributes<string, object>(attributes);

            if (fileid.Length != 0)
            {
                builder.InnerHtml = yq.GetService.Create<yq.IService.IUpload>().DownFileTxt(fileid);
            }

            return new MvcHtmlString( builder.ToString());
        }



        /// <summary>
        /// 编辑器
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <typeparam name="TProperty"></typeparam>
        /// <param name="htmlHelper"></param>
        /// <param name="expression"></param>
        /// <param name="fileid">显示原来文本Id</param>
        /// <param name="modelId">上传时的文件关联ID</param>
        /// <param name="isSimple"></param>
        /// <param name="htmlAttributes"></param>
        /// <returns></returns>
        public static HtmlString Edit<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, System.Linq.Expressions.Expression<Func<TModel, TProperty>> expression,
            string fileid="",
             string modelId = "",
            bool isSimple=false,
            object htmlAttributes = null)
        {
            IDictionary<string, object> attributes = HtmlExtensions.GetHtmlAttributes(htmlAttributes);

            HtmlExtensions.SetHtmlAttributes(ref attributes,"style","width:700px;height:200px;visibility:hidden;");

            string name = ExpressionHelper.GetExpressionText(expression);
            string fullHtmlFieldName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(name);
     
            TagBuilder builder = new TagBuilder("textarea");
            builder.MergeAttribute("id", fullHtmlFieldName, true);
            builder.MergeAttribute("name", fullHtmlFieldName, true);
            builder.MergeAttributes<string, object>(attributes);

            if (fileid.Length != 0)
            {
                builder.InnerHtml = yq.GetService.Create<yq.IService.IUpload>().DownFileTxt(fileid);
            }
            
            //上传文件路径
            string url= UrlHelper.GenerateContentUrl("~/Home/PublicReuse/Upload?ModelId="+modelId,htmlHelper.ViewContext.HttpContext);
          
            StringBuilder sb=new StringBuilder ();
            sb.AppendLine("<script>");
            sb.AppendLine("  KindEditor.ready(function(K) {");
            sb.AppendLine(" K.create('textarea[name=\""+fullHtmlFieldName+"\"]', {");
            sb.AppendFormat(" uploadJson: '{0}', ", url);
            sb.AppendLine(" allowFileManager: false ");
            
            //是否简单模式
            if (isSimple)
            {
                sb.AppendLine(" ,allowFileManager: false, ");
                sb.AppendLine("    resizeType: 1, ");
                sb.AppendLine("    allowPreviewEmoticons: false, ");
                sb.AppendLine("    allowImageUpload: false, ");
                sb.AppendLine("    items: ['fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold', 'italic', 'underline','removeformat', '|', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist','insertunorderedlist', '|', 'emoticons', 'image', 'link'] ");
            }

            sb.AppendLine("} );");
            sb.AppendLine("} );");
            sb.AppendLine("</script>");

            return new MvcHtmlString(sb.ToString() + builder.ToString());
        }


    }
}
